﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace PassWord
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region String

        private void btnEncryptString_Click(object sender, EventArgs e)
        {
            if (!(this.rb3DES_Page1.Checked || this.rbAES_Page1.Checked))
            {
                MessageBox.Show("请选择加密/解密算法");
            }

            if (this.rb3DES_Page1.Checked)
            {
                if (this.tbKey_Page1.Text.Length > 0 && (this.tbKey_Page1.Text.Length != 24 || this.tbKey_Page1.Text.Length != 16))
                {
                    MessageBox.Show("3DES支持的密钥字符串长度为16或24，当前输入长度：" + this.tbKey_Page1.Text.Length);
                    return;
                }

                string pass = this.tbKey_Page1.Text.Length == 0 ? TripleDESHelper.EncryptString(this.tbEncryptString.Text) : TripleDESHelper.EncryptString(this.tbEncryptString.Text, this.tbKey_Page1.Text.Trim());
                this.tbEncryptPassword.Text = pass == this.tbEncryptString.Text ? "加密失败" : pass;
            }

            if (this.rbAES_Page1.Checked)
            {
                int length = this.tbKey_Page1.Text.Length;

                if (this.tbKey_Page1.Text.Length > 0 && !(length == 32 || length == 24 || length == 16))
                {
                    MessageBox.Show("AES支持的密钥字符串长度为32或24或16，当前输入长度：" + this.tbKey_Page1.Text.Length);
                    return;
                }

                string pass = this.tbKey_Page1.Text.Length == 0 ? AESHelper.AESEncrypt(this.tbEncryptString.Text) : AESHelper.AESEncrypt(this.tbEncryptString.Text, this.tbKey_Page1.Text);
                this.tbEncryptPassword.Text = pass == null ? "加密失败" : pass;
            }
        }

        private void btnDecryptString_Click(object sender, EventArgs e)
        {
            if (!(this.rb3DES_Page1.Checked || this.rbAES_Page1.Checked))
            {
                MessageBox.Show("请选择加密/解密算法");
            }

            if (this.rb3DES_Page1.Checked)
            {
                if (this.tbKey_Page1.Text.Length > 0 && (this.tbKey_Page1.Text.Length != 24 || this.tbKey_Page1.Text.Length != 16))
                {
                    MessageBox.Show("3DES支持的密钥字符串长度为16或24，当前输入长度：" + this.tbKey_Page1.Text.Length);
                    return;
                }

                string str = this.tbKey_Page1.Text.Length == 0 ? TripleDESHelper.DecryptString(this.tbDecryptPassword.Text) : TripleDESHelper.DecryptString(this.tbDecryptPassword.Text, this.tbKey_Page1.Text.Trim());
                this.tbDecryptString.Text = str == this.tbDecryptPassword.Text ? "解密失败" : str;
            }

            if (this.rbAES_Page1.Checked)
            {
                int length = this.tbKey_Page1.Text.Length;

                if (this.tbKey_Page1.Text.Length > 0 && !(length == 32 || length == 24 || length == 16))
                {
                    MessageBox.Show("AES支持的密钥字符串长度为32或24或16，当前输入长度：" + this.tbKey_Page1.Text.Length);
                    return;
                }

                string str = this.tbKey_Page1.Text.Length == 0 ? AESHelper.AESDecrypt(this.tbDecryptPassword.Text) : AESHelper.AESDecrypt(this.tbDecryptPassword.Text, this.tbKey_Page1.Text);
                this.tbDecryptString.Text = str == null ? "解密失败" : str;
            }
        }

        #endregion

        #region File

        private void btnEncryptSour_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ValidateNames = true;
            this.openFileDialog1.Filter = "文本文件(*.txt)|*.txt|日志文件(*.log)|*.log|所有文件(*.*)|*.*";
            this.openFileDialog1.AddExtension = true;
            this.openFileDialog1.FilterIndex = 1;
            this.openFileDialog1.FileName = "";

            if (this.openFileDialog1.ShowDialog() == DialogResult.Cancel) return;
            else this.tbEncryptSour.Text = this.openFileDialog1.FileName;
        }

        private void btnEncryptDest_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.ValidateNames = true;
            this.saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|日志文件(*.log)|*.log|所有文件(*.*)|*.*";
            if (this.openFileDialog1.FileName.ToLower().EndsWith(".txt")) this.saveFileDialog1.FilterIndex = 1;
            else if (this.openFileDialog1.FileName.ToLower().EndsWith(".log")) this.saveFileDialog1.FilterIndex = 2;
            else this.saveFileDialog1.FilterIndex = 3;
            this.saveFileDialog1.AddExtension = false;
            this.saveFileDialog1.SupportMultiDottedExtensions = true;
            this.saveFileDialog1.FileName = this.openFileDialog1.FileName == "" ? "" : Path.GetFileNameWithoutExtension(this.openFileDialog1.FileName) + "(已加密)" + Path.GetExtension(this.openFileDialog1.FileName);

            if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel) return;
            else this.tbEncryptDest.Text = this.saveFileDialog1.FileName;
        }

        private void btnEncryptFile_Click(object sender, EventArgs e)
        {
            if (!(this.rb3DES_Page1.Checked || this.rbAES_Page1.Checked))
            {
                MessageBox.Show("请选择加密/解密算法");
            }

            if (this.tbEncryptSour.Text.Length == 0)
            {
                MessageBox.Show("请选择明文文件");
                return;
            }
            if (this.tbEncryptDest.Text.Length == 0)
            {
                MessageBox.Show("请选择文件加密后保存位置");
                return;
            }

            bool b = false;

            if (this.rb3DES_Page2.Checked)
            {
                if (this.tbKey_Page2.Text.Length > 0 && (this.tbKey_Page2.Text.Length != 24 || this.tbKey_Page2.Text.Length != 16))
                {
                    MessageBox.Show("3DES支持的密钥字符串长度为16或24，当前输入长度：" + this.tbKey_Page2.Text.Length);
                    return;
                }

                b = this.tbKey_Page2.Text.Length == 0 ? TripleDESHelper.EncryptFile(this.tbEncryptSour.Text, this.tbEncryptDest.Text) : TripleDESHelper.EncryptFile(this.tbEncryptSour.Text, this.tbEncryptDest.Text, this.tbKey_Page2.Text.Trim());
            }

            if (this.rbAES_Page2.Checked)
            {
                int length = this.tbKey_Page2.Text.Length;

                if (this.tbKey_Page2.Text.Length > 0 && (length == 32 || length == 24 || length == 16))
                {
                    MessageBox.Show("AES支持的密钥字符串长度为32或24或16，当前输入长度：" + this.tbKey_Page2.Text.Length);
                    return;
                }

                b = this.tbKey_Page2.Text.Length == 0 ? AESHelper.AESEncryptFile(this.tbEncryptSour.Text, this.tbEncryptDest.Text) : AESHelper.AESEncryptFile(this.tbEncryptSour.Text, this.tbEncryptDest.Text, this.tbKey_Page2.Text.Trim());
            }

            if (b)
            {
                MessageBox.Show("加密成功");
            }
            else
            {
                MessageBox.Show("操作失败");
            }
        }

        private void btnDecryptSour_Click(object sender, EventArgs e)
        {
            this.openFileDialog2.ValidateNames = true;
            this.openFileDialog2.Filter = "文本文件(*.txt)|*.txt|日志文件(*.log)|*.log|所有文件(*.*)|*.*";
            this.openFileDialog2.FilterIndex = 1;
            this.openFileDialog2.FileName = "";

            if (this.openFileDialog2.ShowDialog() == DialogResult.Cancel) return;
            else this.tbDecryptSour.Text = this.openFileDialog2.FileName;
        }

        private void btnDecryptDest_Click(object sender, EventArgs e)
        {
            this.saveFileDialog2.ValidateNames = true;
            this.saveFileDialog2.Filter = "文本文件(*.txt)|*.txt|日志文件(*.log)|*.log|所有文件(*.*)|*.*";
            if (this.openFileDialog2.FileName.ToLower().EndsWith(".txt")) this.saveFileDialog2.FilterIndex = 1;
            else if (this.openFileDialog2.FileName.ToLower().EndsWith(".log")) this.saveFileDialog2.FilterIndex = 2;
            else this.saveFileDialog2.FilterIndex = 3;
            this.saveFileDialog2.FileName = this.openFileDialog2.FileName == "" ? "" : Path.GetFileNameWithoutExtension(this.openFileDialog2.FileName) + "(已解密)" + Path.GetExtension(this.openFileDialog2.FileName);

            if (this.saveFileDialog2.ShowDialog() == DialogResult.Cancel) return;
            else this.tbDecryptDest.Text = this.saveFileDialog2.FileName;
        }

        private void btnDecryptFile_Click(object sender, EventArgs e)
        {
            if (!(this.rb3DES_Page1.Checked || this.rbAES_Page1.Checked))
            {
                MessageBox.Show("请选择加密/解密算法");
            }

            if (this.tbDecryptSour.Text.Length == 0)
            {
                MessageBox.Show("请选择密文文件");
                return;
            }
            if (this.tbDecryptDest.Text.Length == 0)
            {
                MessageBox.Show("请选择文件解密后保存位置");
                return;
            }

            bool b = false; ;

            if (this.rb3DES_Page2.Checked)
            {
                if (this.tbKey_Page2.Text.Length > 0 && (this.tbKey_Page2.Text.Length != 24 || this.tbKey_Page2.Text.Length != 16))
                {
                    MessageBox.Show("3DES支持的密钥字符串长度为16或24，当前输入长度：" + this.tbKey_Page2.Text.Length);
                    return;
                }

                b = this.tbKey_Page2.Text.Length == 0 ? TripleDESHelper.DecryptFile(this.tbDecryptSour.Text, this.tbDecryptDest.Text) : TripleDESHelper.DecryptFile(this.tbDecryptSour.Text, this.tbDecryptDest.Text, this.tbKey_Page2.Text.Trim());
            }

            if (this.rbAES_Page2.Checked)
            {
                int length = this.tbKey_Page2.Text.Length;

                if (this.tbKey_Page1.Text.Length > 0 && !(length == 32 || length == 24 || length == 16))
                {
                    MessageBox.Show("AES支持的密钥字符串长度为32或24或16，当前输入长度：" + this.tbKey_Page2.Text.Length);
                    return;
                }

                b = this.tbKey_Page2.Text.Length == 0 ? AESHelper.AESDecryptFile(this.tbDecryptSour.Text, this.tbDecryptDest.Text) : AESHelper.AESDecryptFile(this.tbDecryptSour.Text, this.tbDecryptDest.Text, this.tbKey_Page2.Text.Trim());
            }

            if (b)
            {
                MessageBox.Show("解密成功");
            }
            else
            {
                MessageBox.Show("操作失败");
            }
        }

        #endregion

        #region Password

        private void btnCreateKey_Click(object sender, EventArgs e)
        {
            this.tb3DESKey24.Text = Password.Create(24);
            this.tb3DESIV8.Text = Password.Create(8, true, true, true, true);

            if (this.rbtn128.Checked)
            {
                this.tbAESKey32.Text = Password.Create(16);
                this.tbAESIV16.Text = Password.Create(16, true, true, true, true);
            }
            if (this.rbtn192.Checked)
            {
                this.tbAESKey32.Text = Password.Create(24);
                this.tbAESIV16.Text = Password.Create(16, true, true, true, true);
            }
            if (this.rbtn256.Checked)
            {
                this.tbAESKey32.Text = Password.Create(32);
                this.tbAESIV16.Text = Password.Create(16, true, true, true, true);
            }
        }

        private void tabPage3_Enter(object sender, EventArgs e)
        {
            if (this.tb3DESKey24.Text.Length == 0 && this.tb3DESIV8.Text.Length == 0 && this.tbAESKey32.Text.Length == 0 && tbAESIV16.Text.Length == 0)
            {
                btnCreateKey_Click(sender, e);
            }
        }

        #endregion

        public class TripleDESHelper
        {
            private static string sKey = @"MKEaFaW.#=?=7gKv[l/E6n\m"; //私钥
            private static string sIV = @"nH<Q-nqE";

            #region 加密

            /// <summary>
            /// 加密文件
            /// </summary>
            /// <param name="inFileName">输入文件</param>
            /// <param name="outFileName">输出文件</param>
            /// <param name="Key">加密密钥</param>
            /// <param name="IV">向量</param>
            /// <returns>bool</returns>
            public static bool EncryptFile(string inFileName, string outFileName, string Key, string IV)
            {
                FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
                FileStream fout = null;
                try
                {
                    fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }
                fout.SetLength(0);

                byte[] bin = new byte[100];
                long rdlen = 0;
                long totlen = fin.Length;
                int len;

                try
                {
                    TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
                    DES.Key = Encoding.UTF8.GetBytes(Key);
                    DES.IV = Encoding.UTF8.GetBytes(IV);
                    CryptoStream encStream = new CryptoStream(fout, DES.CreateEncryptor(), CryptoStreamMode.Write);

                    while (rdlen < totlen)
                    {
                        len = fin.Read(bin, 0, 100);
                        encStream.Write(bin, 0, len);
                        rdlen = rdlen + len;
                    }

                    encStream.Close();
                }
                catch
                {
                    return false;
                }

                return true;
            }

            /// <summary>
            /// 加密文件
            /// </summary>
            /// <param name="inFileName">输入文件</param>
            /// <param name="outFileName">输出文件</param>
            /// <param name="Key">加密密钥</param>
            /// <returns>bool</returns>
            public static bool EncryptFile(string inFileName, string outFileName, string Key)
            {
                return EncryptFile(inFileName, outFileName, Key, sIV);
            }

            /// <summary>
            /// 加密文件
            /// </summary>
            /// <param name="inFileName">输入文件</param>
            /// <param name="outFileName">输出文件</param>
            /// <returns>bool</returns>
            public static bool EncryptFile(string inFileName, string outFileName)
            {
                return EncryptFile(inFileName, outFileName, sKey, sIV);
            }

            /// <summary>
            /// 加密字符串
            /// </summary>
            /// <param name="encryptValue">待加密的字符串</param>
            /// <param name="key">加密密钥</param>
            /// <param name="Key">向量</param>
            /// <returns>加密成功返回加密后的字符串，失败返回源字符串</returns>
            public static string EncryptString(string encryptValue, string Key, string IV)
            {
                try
                {
                    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                    des.Key = Encoding.UTF8.GetBytes(Key);
                    des.IV = Encoding.UTF8.GetBytes(IV);
                    des.Mode = CipherMode.ECB;
                    des.Padding = PaddingMode.PKCS7;

                    byte[] valBytes = Encoding.UTF8.GetBytes(encryptValue);
                    ICryptoTransform transform = des.CreateEncryptor();

                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);


                    cs.Write(valBytes, 0, valBytes.Length);
                    cs.FlushFinalBlock();
                    byte[] returnBytes = ms.ToArray();
                    cs.Close();
                    ms.Close();

                    return Convert.ToBase64String(returnBytes);
                }
                catch
                {
                    return encryptValue;
                }
            }

            /// <summary>
            /// 加密字符串
            /// </summary>
            /// <param name="encryptValue">待加密的字符串</param>
            /// <param name="Key">加密密钥</param>
            /// <returns>加密成功返回加密后的字符串，失败返回源字符串</returns>
            public static string EncryptString(string encryptValue, string Key)
            {
                return EncryptString(encryptValue, Key, sIV);
            }

            /// <summary>
            /// 加密字符串
            /// </summary>
            /// <param name="encryptValue">待加密的字符串</param>
            /// <returns>加密成功返回加密后的字符串，失败返回源字符串</returns>
            public static string EncryptString(string encryptValue)
            {
                return EncryptString(encryptValue, sKey, sIV);
            }

            #endregion

            #region 解密

            /// <summary>
            /// 解密文件
            /// </summary>
            /// <param name="inFileName">输入文件</param>
            /// <param name="outFileName">输入文件</param>
            /// <param name="Key">解密密钥</param>
            /// <param name="IV">向量</param>
            /// <returns>bool</returns>
            public static bool DecryptFile(string inFileName, string outFileName, string Key, string IV)
            {
                FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
                FileStream fout = null;
                try
                {
                    fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }
                fout.SetLength(0);

                byte[] bin = new byte[100];
                long rdlen = 0;
                long totlen = fin.Length;
                int len;

                try
                {
                    TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
                    DES.Key = Encoding.UTF8.GetBytes(Key);
                    DES.IV = Encoding.UTF8.GetBytes(IV);
                    CryptoStream decStream = new CryptoStream(fout, DES.CreateDecryptor(), CryptoStreamMode.Write);

                    while (rdlen < totlen)
                    {
                        len = fin.Read(bin, 0, 100);
                        decStream.Write(bin, 0, len);
                        rdlen = rdlen + len;
                    }

                    decStream.Close();
                }
                catch
                {
                    return false;
                }

                return true;
            }

            /// <summary>
            /// 解密文件
            /// </summary>
            /// <param name="inFileName">输入文件</param>
            /// <param name="outFileName">输入文件</param>
            /// <param name="Key">解密密钥</param>
            /// <returns>bool</returns>
            public static bool DecryptFile(string inFileName, string outFileName, string Key)
            {
                return DecryptFile(inFileName, outFileName, Key, sIV);
            }

            /// <summary>
            /// 解密文件
            /// </summary>
            /// <param name="inFileName">输入文件</param>
            /// <param name="outFileName">输入文件</param>
            /// <returns>bool</returns>
            public static bool DecryptFile(string inFileName, string outFileName)
            {
                return DecryptFile(inFileName, outFileName, sKey, sIV);
            }

            /// <summary>
            /// 解密字符串
            /// </summary>
            /// <param name="decryptedValue">待解密的字符串</param>
            /// <param name="Key">解密密钥</param>
            /// <param name="IV">向量</param>
            /// <returns>解密成功返回解密后的字符串，失败返回源字符串</returns>
            public static string DecryptString(string decryptedValue, string Key, string IV)
            {
                try
                {
                    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                    des.Key = Encoding.UTF8.GetBytes(Key);
                    des.IV = Encoding.UTF8.GetBytes(IV);
                    des.Mode = CipherMode.ECB;
                    des.Padding = PaddingMode.PKCS7;

                    byte[] valBytes = Convert.FromBase64String(decryptedValue);
                    ICryptoTransform transform = des.CreateDecryptor();

                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
                    cs.Write(valBytes, 0, valBytes.Length);
                    cs.FlushFinalBlock();
                    byte[] returnBytes = ms.ToArray();
                    cs.Close();
                    ms.Close();

                    return Encoding.UTF8.GetString(returnBytes);
                }
                catch
                {
                    return decryptedValue;
                }
            }

            /// <summary>
            /// 解密字符串
            /// </summary>
            /// <param name="decryptedValue">待解密的字符串</param>
            /// <param name="key">解密密钥</param>
            /// <returns>解密成功返回解密后的字符串，失败返回源字符串</returns>
            public static string DecryptString(string decryptedValue, string Key)
            {
                return DecryptString(decryptedValue, Key, sIV);
            }

            /// <summary>
            /// 解密字符串
            /// </summary>
            /// <param name="decryptedValue">待解密的字符串</param>
            /// <returns>解密成功返回解密后的字符串，失败返回源字符串</returns>
            public static string DecryptString(string decryptedValue)
            {
                return DecryptString(decryptedValue, sKey, sIV);
            }

            #endregion
        }

        public class AESHelper
        {
            private static string sKey = @"Q'\rjKE}|<\]#oa:\T.]NJz9D?`$}3Oz";
            private static string sIV = @"zTJQ{i5NOAL~aoea";

            #region 加密

            /// <summary>
            /// AES加密文件
            /// </summary>
            /// <param name="inFile">输入文件</param>
            /// <param name="outFile">输入文件</param>
            /// <param name="Key">加密密钥</param>
            /// <param name="Vector">向量</param>
            /// <returns>bool</returns>
            public static bool AESEncryptFile(string inFile, string outFile, string Key, string Vector)
            {
                FileStream fin = new FileStream(inFile, FileMode.Open, FileAccess.Read);
                FileStream fout = null;
                try
                {
                    fout = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }
                fout.SetLength(0);

                byte[] bin = new byte[100];
                long rdlen = 0;
                long totlen = fin.Length;
                int len;

                Rijndael Aes = Rijndael.Create();
                Aes.Key = Encoding.UTF8.GetBytes(Key);
                Aes.IV = Encoding.UTF8.GetBytes(Vector);

                try
                {
                    CryptoStream encStream = new CryptoStream(fout, Aes.CreateEncryptor(), CryptoStreamMode.Write);

                    while (rdlen < totlen)
                    {
                        len = fin.Read(bin, 0, 100);
                        encStream.Write(bin, 0, len);
                        rdlen = rdlen + len;
                    }

                    encStream.Close();
                }
                catch
                {
                    return false;
                }

                return true;
            }

            /// <summary>
            /// AES加密文件
            /// </summary>
            /// <param name="inFile">输入文件</param>
            /// <param name="outFile">输入文件</param>
            /// <param name="Key">加密密钥</param>
            /// <returns>bool</returns>
            public static bool AESEncryptFile(string inFile, string outFile, string Key)
            {
                return AESEncryptFile(inFile, outFile, Key, sIV);
            }

            /// <summary>
            /// AES加密文件
            /// </summary>
            /// <param name="inFile">输入文件</param>
            /// <param name="outFile">输入文件</param>
            /// <returns>bool</returns>
            public static bool AESEncryptFile(string inFile, string outFile)
            {
                return AESEncryptFile(inFile, outFile, sKey, sIV);
            }

            /// <summary>
            /// AES加密字符串
            /// </summary>
            /// <param name="Data">被加密的明文</param>
            /// <param name="Key">密钥</param>
            /// <param name="Vector">向量</param>
            /// <returns>密文</returns>
            public static string AESEncrypt(string encryptValue, String Key, String Vector)
            {

                Byte[] Cryptograph = null; // 加密后的密文
                Byte[] Data = Encoding.UTF8.GetBytes(encryptValue);

                MemoryStream ms = new MemoryStream();

                Rijndael Aes = Rijndael.Create();
                Aes.Key = Encoding.UTF8.GetBytes(Key);
                Aes.IV = Encoding.UTF8.GetBytes(Vector);
                try
                {
                    CryptoStream cs = new CryptoStream(ms, Aes.CreateEncryptor(), CryptoStreamMode.Write);
                    cs.Write(Data, 0, Data.Length);
                    cs.FlushFinalBlock();
                    Cryptograph = ms.ToArray();
                    cs.Close();
                    ms.Close();
                }
                catch
                {
                    return null;
                }

                return Convert.ToBase64String(Cryptograph);
            }

            /// <summary>
            /// AES加密字符串
            /// </summary>
            /// <param name="encryptValue">被加密的明文</param>
            /// <param name="Key">密钥</param>
            /// <returns>密文</returns>
            public static string AESEncrypt(string encryptValue, String Key)
            {
                return AESEncrypt(encryptValue, Key, sIV);
            }

            /// <summary>
            /// AES加密字符串
            /// </summary>
            /// <param name="encryptValue">被加密的明文</param>
            /// <returns>密文</returns>
            public static string AESEncrypt(string encryptValue)
            {
                return AESEncrypt(encryptValue, sKey, sIV);
            }

            #endregion

            #region 解密

            /// <summary>
            /// AES解密文件
            /// </summary>
            /// <param name="inFile">输入文件</param>
            /// <param name="outFile">输入文件</param>
            /// <param name="Key">加密密钥</param>
            /// <param name="Vector">向量</param>
            /// <returns>bool</returns>
            public static bool AESDecryptFile(string inFile, string outFile, string Key, string Vector)
            {
                FileStream fin = new FileStream(inFile, FileMode.Open, FileAccess.Read);
                FileStream fout = null;
                try
                {
                    fout = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }
                fout.SetLength(0);

                byte[] bin = new byte[100];
                long rdlen = 0;
                long totlen = fin.Length;
                int len;

                Rijndael Aes = Rijndael.Create();
                Aes.Key = Encoding.UTF8.GetBytes(Key);
                Aes.IV = Encoding.UTF8.GetBytes(Vector);

                try
                {
                    CryptoStream decStream = new CryptoStream(fout, Aes.CreateDecryptor(), CryptoStreamMode.Write);

                    while (rdlen < totlen)
                    {
                        len = fin.Read(bin, 0, 100);
                        decStream.Write(bin, 0, len);
                        rdlen = rdlen + len;
                    }

                    decStream.Close();
                }
                catch
                {
                    return false;
                }

                return true;
            }

            /// <summary>
            /// AES解密文件
            /// </summary>
            /// <param name="inFile">输入文件</param>
            /// <param name="outFile">输入文件</param>
            /// <param name="Key">加密密钥</param>
            /// <returns>bool</returns>
            public static bool AESDecryptFile(string inFile, string outFile, string Key)
            {
                return AESDecryptFile(inFile, outFile, Key, sIV);
            }

            /// <summary>
            /// AES解密文件
            /// </summary>
            /// <param name="inFile">输入文件</param>
            /// <param name="outFile">输入文件</param>
            /// <returns>bool</returns>
            public static bool AESDecryptFile(string inFile, string outFile)
            {
                return AESDecryptFile(inFile, outFile, sKey, sIV);
            }

            /// <summary>
            /// AES解密字符串
            /// </summary>
            /// <param name="Data">被解密的密文</param>
            /// <param name="Key">密钥</param>
            /// <param name="Vector">向量</param>
            /// <returns>明文</returns>
            public static string AESDecrypt(String decryptedValue, String Key, String Vector)
            {
                Byte[] original = null; // 解密后的明文 
                Byte[] Data = Convert.FromBase64String(decryptedValue);

                MemoryStream ms = new MemoryStream();

                Rijndael Aes = Rijndael.Create();
                Aes.Key = Encoding.UTF8.GetBytes(Key);
                Aes.IV = Encoding.UTF8.GetBytes(Vector);
                try
                {
                    CryptoStream cs = new CryptoStream(ms, Aes.CreateDecryptor(), CryptoStreamMode.Write);
                    cs.Write(Data, 0, Data.Length);
                    cs.FlushFinalBlock();
                    original = ms.ToArray();
                    cs.Close();
                    ms.Close();
                }
                catch
                {
                    return null;
                }

                return Encoding.UTF8.GetString(original);
            }

            /// <summary>
            /// AES解密字符串
            /// </summary>
            /// <param name="decryptedValue">被解密的密文</param>
            /// <param name="Key">密钥</param>
            /// <returns>明文</returns>
            public static string AESDecrypt(string decryptedValue, string Key)
            {
                return AESDecrypt(decryptedValue, Key, sIV);
            }

            /// <summary>
            /// AES解密字符串
            /// </summary>
            /// <param name="decryptedValue">被解密的密文</param>
            /// <returns>明文</returns>
            public static string AESDecrypt(string decryptedValue)
            {
                return AESDecrypt(decryptedValue, sKey, sIV);
            }

            #endregion
        }

        public class Password
        {
            private static string[] Pass = {
                                               "1","2","3","4","5","6","7","8","9","0",    //1-10
                                                "a","b","c","d","e","f","g","h","i", "j",   //11-20
                                                "k","l","m","n","o","p","q","r","s","t",    //21-30
                                                "u","v","w","x","y","z","A","B","C","D",    //31-40
                                                "E","F","G","H","I","J","K","L","M","N",    //41-50
                                                "O","P","Q","R","S","T","U","V","W","X",    //51-60
                                                "Y","Z","~","!","@","#","$","%","^","*",    //61-70
                                                "(",")","_","+","-","=","{","}","[", "]",   //71-80
                                                "\\","|",":",";","'",",",".","/","?","`"    //81-90
                                           };
            private static string[] Number = { 
                                                 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" 
                                             };
            private static string[] Sign = { 
                                               "~", "!", "@", "#", "$", "%", "^", "*", "(", ")",
                                               "_", "+", "-", "=", "{", "}", "[", "]", "\\", "|", 
                                               ":", ";", "'", ",", ".", "/", "?", "`" 
                                           };
            private static string[] Sword = { 
                                                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 
                                                "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                                                "u", "v", "w", "x", "y", "z" 
                                            };
            private static string[] Bword = { 
                                                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
                                                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", 
                                                "U", "V", "W", "X", "Y", "Z" 
                                            };

            /// <summary>
            /// 生成指定长度的随机密码
            /// </summary>
            /// <param name="length">密码长度</param>
            /// <returns>string</returns>
            public static string Create(int length)
            {
                string pass = "";
                Random Ran = new Random();
                for (int i = 0; i < length; i++)
                {
                    pass += Pass[Ran.Next(0, Pass.Length)];
                }
                return pass;
            }

            /// <summary>
            /// 生成指定长度的随机密码
            /// </summary>
            /// <param name="length">密码长度</param>
            /// <param name="number">是否包含数字</param>
            /// <param name="sign">是否包含特殊符号</param>
            /// <param name="smallword">是否包含小写字母</param>
            /// <param name="bigword">是否包含大写字母</param>
            /// <returns>string</returns>
            public static string Create(int length, bool number, bool sign, bool smallword, bool bigword)
            {
                Random Ran = new Random();
                int intA = 0;
                string pass = "";
                for (int i = 0; i < length; i++)
                {
                    //生成随机数A，表示生成类型。1=数字，2=符号，3=小写字母，4=大写字母
                    intA = Ran.Next(1, 5);
                    if (intA == 1 && number)
                    {
                        pass += Number[Ran.Next(0, Number.Length)];
                        continue;
                    }
                    if (intA == 2 && sign)
                    {
                        pass += Sign[Ran.Next(0, Sign.Length)];
                        continue;
                    }
                    if (intA == 3 && smallword)
                    {
                        pass += Sword[Ran.Next(0, Sword.Length)];
                        continue;
                    }
                    if (intA == 4 && smallword)
                    {
                        pass += Bword[Ran.Next(0, Bword.Length)];
                        continue;
                    }
                }

                return pass;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "";
            this.openFileDialog2.FileName = "";
            this.saveFileDialog1.FileName = "";
            this.saveFileDialog2.FileName = "";
        }
    }
}
